#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <memory>
#include <set>

#include "Noncopyable.h"
#include "Acceptor.h"
#include "callbacks.h"
#include "InetAddress.h"
#include "EventLoopThreadPool.h"

class EventLoop;

class TcpServer : Noncopyable {
public:
    TcpServer(EventLoop* loop, InetAddress& serverAddr);
    ~TcpServer();

    void start();

    void setThreadNum(int numThread) {
        threadPool_.setThreadNum(numThread);
    }

    void setConnectionCallback(const ConnectionCallback& cb) {
        connectionCallback_ = std::move(cb);
    }

    void setMessageCallback(const MessageCallback& cb) {
        messageCallback_ = std::move(cb);
    }

    void setWriteCompleteCallback(const WriteCompleteCallback& cb) {
        writeCompleteCallback_ = std::move(cb);
    }


private:

    void newConnectionCallback(int connfd, InetAddress& peerAddr);

    void removeConnection(TcpConnectionPtr conn);

    EventLoop* loop_;
    std::unique_ptr<Acceptor> acceptor_;
    InetAddress serverAddr_;
    EventLoopThreadPool threadPool_;

    typedef std::set<TcpConnectionPtr> ConnectionList;
    ConnectionList connections_;

    ConnectionCallback connectionCallback_;
    MessageCallback messageCallback_;
    WriteCompleteCallback writeCompleteCallback_;

};


#endif